package com.jsmile.cloud.accountcenter.account.feign;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.accountcenter.account.service.MemberApplicationService;
import com.jsmile.cloud.accountcenter.api.req.ReqMemberDetial;
import com.jsmile.cloud.accountcenter.api.req.account.ReqMemberQuery;
import com.jsmile.cloud.accountcenter.api.res.ResMember;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.to.JsmilePage;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
public class MemberServiceApiImpl extends AuthcController {

    private final MemberApplicationService memberApplicationService;

    @RequestMapping(value = "/feign/member/getMember", method = RequestMethod.POST)
    public JSmileResult getMember(@RequestBody @Valid ReqMemberDetial reqMemberDetial, @CurrentContext JsmileContext jsmileContext) {
        ResMember member = memberApplicationService.getMember(reqMemberDetial.getUserId(), jsmileContext.getTenantId());
        return JSmileResult.success(member);
    }

    @RequestMapping(value = "/feign/member/getMemberPageList", method = RequestMethod.POST)
    public JSmileResult<JsmilePage<ResMember>> getMemberPageList(@RequestBody @Valid ReqMemberQuery reqMemberQuery, @CurrentContext JsmileContext jsmileContext) {
        reqMemberQuery.setCurTenantId(jsmileContext.getTenantId());
        IPage<ResMember> pages = memberApplicationService.getMemberPageList(reqMemberQuery);
        return JSmileResult.success(new JsmilePage(reqMemberQuery.getPageIndex(), reqMemberQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }

}
